package me.hl.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.Arrays;

/**
 * Created by yy on 2016/8/2.
 */
@Aspect
@Component
public class LogPrintAspect {

    private static Logger logger = LoggerFactory.getLogger(LogPrintAspect.class);

    /*    @AfterReturning(value = "@annotation(me.hl.aspect.Planck)", returning = "returnValue", pointcut = "execution(* com.travel.service..*.*(..))")
        public void beforePrint(JoinPoint joinPoint, Object returnValue) {
            logger.info("-----------------------------------------------------------------");
            MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
            System.out.println("切点名称" + methodSignature.getMethod().getAnnotation(Planck.class).name());
            logger.info("方法名 = {}", joinPoint.getSignature().getDeclaringTypeName());
            logger.info("收到参数={}", Arrays.toString(joinPoint.getArgs()));
            logger.info("返回值={}", returnValue);
            logger.info("-----------------------------------------------------------------");
        }*/

    @Around(value = "@annotation(planck)")
    public Object beforePrint(ProceedingJoinPoint pjp, Planck planck) {
        Object result = null;
        try {
            logger.info("-----------------------------------------------------------------");
            long stratTime = System.currentTimeMillis();
            logger.info("planck.name = {}", planck.name());
            logger.info("The method {} begins with {}", pjp.getSignature().getName(), stratTime);
            logger.info("method args = {}", Arrays.toString(pjp.getArgs()));
            result = pjp.proceed();
            logger.info("result = {}", result);
            logger.info("cost time {}", System.currentTimeMillis() - stratTime);
            logger.info("-----------------------------------------------------------------");
        } catch (Throwable throwable) {
            logger.info("{} occur error {}", pjp.getSignature().getName(), throwable);
        }
        return result;
    }
}
